<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Gamelog extends Model
{
    public function student()
    {
        return $this->belongsTo(Student::class, 'student_id');
    }

    public function school()
    {
        return $this->belongsTo(School::class);
    }

    public function getStatistic()
    {
        if (isset($this->game_data)){
            $data = json_decode($this->game_data, true);
            if (count($data) == 1){
                $this->question_id = $data[0]['question_id'];
                $this->isCorrect = $data[0]['isCorrect'];
            }
            $this->name = optional($this->student)->name;
            $this->age = optional($this->student)->age;
            $this->sex = optional($this->student)->sex;
            unset($this->student);
            unset($this->game_data);
        }
        return $this;
    }
}
